package com.rmb.controller;
import com.rmb.annotation.SystemLog;
import com.rmb.util.EngineResult;
import com.rmb.common.model.response.ResponseResult;
import com.rmb.entity.EtlDataEngine;
import com.rmb.entity.EtlDataEngineVoResult;
import com.rmb.enums.LogType;
import com.rmb.service.EtlDataEngineService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (EtlDataEngine)表控制层
 *
 * @author makejava
 * @since 2021-01-13 10:24:40
 */
@RestController
@RequestMapping("/etlDataEngine")
public class EtlDataEngineController {
    /**
     * 服务对象
     */
    @Resource
    private EtlDataEngineService etlDataEngineService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    @SystemLog(description = "通过主键查询单条数据",type = LogType.SELETE)
    public EtlDataEngine selectOne(Long id) {
        return this.etlDataEngineService.queryById(id);
    }

    /**
     * 分页查询
     * @param page 第几页
     * @param size 每页多少条
     * @return findList
     */
    @GetMapping("findList/{page}/{size}")
    @SystemLog(description = "分页查询",type = LogType.SELETE)
    public List<EtlDataEngine> findList(@PathVariable("page") int page,@PathVariable("size")int size){
        return etlDataEngineService.queryAllByLimit(page,size);
    }



    /**
     * 添加數據引擎
     * @param etlDataEngine 数据
     * @return insert
     */
    @PostMapping("insert")
    @SystemLog(description = "添加數據引擎",type = LogType.INSERT)
    public EtlDataEngine insert(@RequestBody EtlDataEngine etlDataEngine){
        return etlDataEngineService.insert(etlDataEngine);
    }

    /**
     * 根据ID请求数据
     * @param id id
     * @return getEngineId
     */
    @GetMapping("getEngineId/{id}")
    @SystemLog(description = "根据ID请求数据",type = LogType.SELETE)
    public EtlDataEngine getEngineId(@PathVariable("id")Long id){
        return etlDataEngineService.getEngineId(id);
    }

    /**
     * 编译代码
     * @param id id
     * @param compileJava 数据代码
     * @return compile
     */
    @PutMapping("compile")
    @SystemLog(description = "编译代码",type = LogType.UPDATE)
    public ResponseResult compile(Long id, String compileJava){

        return etlDataEngineService.compile(id,compileJava);
    }
    /**
     *  查询所有
     *   模糊
     * @param page 查询起始位置
     * @param size 查询条数
     * @param engineName 引擎名称
     * @param engineTypeId 引擎ID
     * @param codeType 类型
     * @return selectEtlEngine
     */
    @GetMapping("/list/{page}/{size}")
    @SystemLog(description = "双表查询加模糊",type = LogType.SELETE)
    public List<EtlDataEngineVoResult> list(@PathVariable("page") int page,
                                            @PathVariable("size")int size,
                                            String engineName, String engineTypeId, String codeType){
        return etlDataEngineService.list(page,size,engineName,engineTypeId,codeType);
    }

    /**
     * 请求参数：EtlDataEngine对象
     * 修改方法 类函数不可修改
     * @param etlDataEngine 数据
     * @return Update
     */
    @PostMapping("/Update")
    @SystemLog(description = "修改方法，类函数不可修改",type = LogType.UPDATE)
    public ResponseResult update(@RequestBody EtlDataEngine etlDataEngine){
        return etlDataEngineService.updateEtlDataEngine(etlDataEngine);
    }

    /**
     * 请求参数： 数据id
     * 删除方法
     * @param id id
     * @return Delete
     */
    @PutMapping("/Delete/{id}")
    @SystemLog(description = "删除方法",type = LogType.DELETE)
    public ResponseResult delete(@PathVariable("id") long id){
        return etlDataEngineService.deleteById(id);
    }

    /**
     * 根据ID回显对象
     * @param id id
     * @return 结果集
     */
    @GetMapping("/findById/{id}")
    @SystemLog(description = "根据ID回显对象",type = LogType.SELETE)
    public EngineResult findById(@PathVariable("id") Long id){
        EtlDataEngine etlDataEngine = etlDataEngineService.findById(id);
        //判断对象是否存在
        if (etlDataEngine == null){
            return new EngineResult(false,null);
        }
        return new EngineResult(true,etlDataEngine);
    }

    /**
     * 修改引擎代码
     * @param id id
     * @param javaString 数据
     */
    @PutMapping("/compileJava/{id}")
    @SystemLog(description = "修改引擎代码",type = LogType.UPDATE)
    public EngineResult update(@PathVariable("id") Long id,String javaString){
        int i = etlDataEngineService.compileJava(id, javaString);
        if (i == 0){
            return new EngineResult(false,null);
        }
        return new EngineResult(true,null);
    }

}